/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.skydoves.preferenceroomdemo;

import com.skydoves.preferenceroomdemo.entities.Preference_UserDevice;
import com.skydoves.preferenceroomdemo.utils.SecurityUtils;
import junit.framework.TestCase;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class DeviceEntityTest extends TestCase {
    private Preferences preference;
    private Preference_UserDevice device;

    private static final String version = "1.0.0.0";
    private static final String uuid = "00001234-0000-0000-0000-000123456789";

    @Test
    public void testVersion() throws Exception {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        device = Preference_UserDevice.getInstance(appContext);
        preference = new DatabaseHelper(appContext).getPreferences(device.getEntityName());
        preference.putString(device.versionKeyName(), version).flush();
        assertThat(device.getVersion(), is(version));
    }

    @Test
    public void testSecurity() throws Exception {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        device = Preference_UserDevice.getInstance(appContext);
        preference = new DatabaseHelper(appContext).getPreferences(device.getEntityName());
        device.putUuid(uuid); // encrypt
        assertThat(device.getUuid(), is(uuid)); // decrypt
        assertThat(preference.getString("uuid", null), is(SecurityUtils.encrypt(uuid)));
    }
}